<template>
    <div id="stack-bar" ref="canvas" class="canvas" :style="{width: '100%', height: canHeight + 'px'}"></div>
</template>
<script setup lang="ts">
import { defineProps, computed, onMounted } from 'vue'
import { createPinia } from 'pinia'
import * as echarts from 'echarts/core';
import {
  TooltipComponent,
  TooltipComponentOption,
  GridComponent,
  GridComponentOption,
  LegendComponent,
  LegendComponentOption
} from 'echarts/components';
import { BarChart, BarSeriesOption } from 'echarts/charts';
import { CanvasRenderer } from 'echarts/renderers';
import { useChartsStore } from '@/stores/charts';
const pinia = createPinia();
const chartStore = useChartsStore(pinia)

echarts.use([
  TooltipComponent,
  GridComponent,
  LegendComponent,
  BarChart,
  CanvasRenderer
]);

type EChartsOption = echarts.ComposeOption<
  | TooltipComponentOption
  | GridComponentOption
  | LegendComponentOption
  | BarSeriesOption
>;
const props = defineProps({
    canvasHeight: {
        type:Number,
        default: 300
    },
    data: {
        type: Object,
        default: () => { } 
    }
})
let canHeight = computed(() => { return props.canvasHeight })
onMounted(() => {
    var chartDom = document.getElementById('stack-bar')!;
    var myChart = echarts.init(chartDom);
    var option: EChartsOption;

    option = {
        color: chartStore.colors,
        tooltip: {
            trigger: 'axis',
            axisPointer: {
            // Use axis to trigger tooltip
            type: 'shadow' // 'shadow' as default; can also be 'line' or 'shadow'
            }
        },
        legend: {
            show: true,
            left: 0,
            align: 'left',
            textStyle: {color: '#fff'},
        },
        grid: {
            top: '10%',
            left: 0,
            right: '2%',
            bottom: 0,
            containLabel: true
        },
        xAxis: {
            type: 'value',
            axisLabel: {
                show: true,
                color: '#1793df'
            },
            
        },
        yAxis: {
            type: 'category',
            data: props.data.xAxisDataList,
            axisLabel: {
                show: true,
                color: '#1793df',
                formatter: function (value) { return chartStore.weekEnToCn[value]; }
            },
        },
        series: props.data.seriesDataList
    };

    option && myChart.setOption(option);
})
</script>

<style scope>
</style>